<template>
	<view class="title-content">
		<view class="config-title config-title-view">
			<view class="index-title">
				AIGC阅读首页
			</view>
			<view class="title-item-view">
				<view class="item-view" @tap.stop="changeIndex(0)">
					<view :class="selectIndex === 0 ? 'item-view-text-sel' : 'item-view-text'">首页</view>
					<view :class="selectIndex === 0 ? 'item-view-line-sel' : 'item-view-line'"></view>
				</view>
				<view class="item-view" @tap.stop="changeIndex(1)">
					<view :class="selectIndex === 1 ? 'item-view-text-sel' : 'item-view-text'">分类</view>
					<view :class="selectIndex === 1 ? 'item-view-line-sel' : 'item-view-line'"></view>
				</view>
				<view class="item-view" @tap.stop="changeIndex(2)">
					<view :class="selectIndex === 2 ? 'item-view-text-sel' : 'item-view-text'">排行</view>
					<view :class="selectIndex === 2 ? 'item-view-line-sel' : 'item-view-line'"></view>
				</view>
				<view class="item-view" @tap.stop="changeIndex(3)">
					<view :class="selectIndex === 3 ? 'item-view-text-sel' : 'item-view-text'">企业阅读</view>
					<view :class="selectIndex === 3 ? 'item-view-line-sel' : 'item-view-line'"></view>
				</view>
				<view class="item-view" @tap.stop="changeIndex(4)">
					<view :class="selectIndex === 4 ? 'item-view-text-sel' : 'item-view-text'">征文大赛</view>
					<view :class="selectIndex === 4 ? 'item-view-line-sel' : 'item-view-line'"></view>
				</view>
				<view class="item-view" @tap.stop="changeIndex(5)">
					<view :class="selectIndex === 5 ? 'item-view-text-sel' : 'item-view-text'">下载</view>
					<view :class="selectIndex === 5 ? 'item-view-line-sel' : 'item-view-line'"></view>
				</view>
			</view>
			<view class="search-view">
				<u-search placeholder="关键词" :show-action="false"></u-search>
			</view>
			<view class="login-button">
				<u-button size="mini" @click="goLogin">{{phone ? phone : '登录'}}</u-button>
				<u-button size="mini" style="margin-left: 30px;" v-if="phone" @tap.stop="outLogin">退出登录</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return { 
				selectIndex: 0,
				phone: uni.getStorageSync('phone') || ''
			}
		},
		created() {
			uni.$on('loginSuccess',this.loginSuccess)
			this.phone = uni.getStorageSync('phone') || ''
		},
		methods: {
			changeIndex(index) {
				if(index !== 5){
					this.selectIndex = index
				}else {
					window.open('#/pages/miniprogram/index')
				}
				if(index === 0){
					this.goIndex()
				}
			},
			goIndex(){
				uni.navigateTo({ url:'/pages/index/index' })
			},
			goLogin(){
				if(this.phone) return 
				uni.navigateTo({ url:'/pages/index/login' })
			},
			loginSuccess(phone){
				this.phone = phone
			},
			outLogin(){
				uni.clearStorageSync()
				uni.showToast({ 
					title:'退出成功',
					success: res => {
						setTimeout(()=>{
							window.location.reload()
						},1500)
					}
				})
			}
		}
	}
</script>

<style scoped>
	.title-content {
		width: 750rpx;
		padding: 40rpx 100rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.config-title-view {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.index-title {
		font-size: 60rpx;
	}

	.title-item-view {
		display: flex;
		justify-content: space-between;
		margin-left: 80rpx;
	}

	.item-view {
		position: relative;
		margin-left: 40rpx;
		cursor: pointer;
	}

	.item-view-text-sel {
		color: red;
	}

	.item-view-text:hover {
		color: red;
	}

	.item-view-line-sel {
		width: 100%;
		height: 6rpx;
		background-color: red;
		position: absolute;
		bottom: -10rpx;
		left: 0;
	}

	.search-view {
		margin-left: 80rpx;
	}

	.login-button {
		margin-left: 60rpx;
	}
</style>